package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 * 根据学号改姓名和qq号
 */
public class UpdateStudent {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
        //          固定：类型://ip+端口/数据库名?其他参数
        String url = "jdbc:mysql://localhost:3308/java82";
        Connection conn = null;
        Statement st = null;
        //输入相关值
        Scanner in = new Scanner(System.in);
        System.out.print("请输入学号：");
        long stuno=in.nextLong();
        in.nextLine();//去掉那个回车
        System.out.print("请输入name：");
       String name=in.nextLine();
       //换掉所有的Sql特殊字符  \
        //name=name.replaceAll("#","\\#");

        System.out.print("请输入qq号：");
        String qq=in.nextLine();
        String sql ="update s_student set sname='"+name+"',qq='"+qq+"' where stuno="+stuno;
        System.out.println(sql);
        try {
            conn = DriverManager.getConnection(url, "root", "root1234");
            st = conn.createStatement();
            int rows = st.executeUpdate(sql);
            System.out.println("成功修改了:" + rows + "行");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

            try {
                if (st != null)
                    st.close();


            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
